package org.softmed.rest.editor.comps;

public class SaveThread implements Runnable {

	private CustomTextField field;
	Thread thread = new Thread(this);

	public SaveThread(CustomTextField field) {
		this.field = field;
	}

	public void start() {
		thread.start();
	}

	public boolean isAlive() {
		return thread.isAlive();
	}

	@Override
	public void run() {
		while (true) {

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!field.isDirty())
				break;

			long currentTime = System.currentTimeMillis();
			long dt = currentTime - field.getLastChange();
			float dtSeconds = dt * 0.001f;
			if (dtSeconds >= CustomTextField.secondsToTriggerSave) {
				field.triggerAction();
				break;
			}

		}
		thread = new Thread(this);
	}
}
